package com.shawn.springboot.integeration.netty.server.handler;

import com.shawn.springboot.integeration.netty.msg.Request;
import com.shawn.springboot.integeration.netty.msg.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhangjian
 * @title: MyServerHandler
 * @projectName 12-springboot学习
 * @description: 服务端处理器
 * @date 2021/4/2810:35
 */
@Slf4j
public class MyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        Request request = (Request) msg;
        log.info("服务端接受到消息[{}]",request);
        Response response = new Response();
        response.setRequestId(request.getRequestId());
        response.setParam(request.getParam() + "请求成功,成功返回信息");
        ctx.writeAndFlush(response);
        ReferenceCountUtil.release(msg);
    }

    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }
    @Override
    public void channelActive(ChannelHandlerContext chx) {
        log.info("[{}]请求建立连接成功",chx.channel().remoteAddress());
    }
    @Override
    public void channelInactive(ChannelHandlerContext chx) {
        log.info("[{}]断开连接",chx.channel().remoteAddress());
    }
}
